
// Copyright (c) 2012, 2013 All Right Reserved, http://hpc.utp.edu.my
// Author: Nordin Zakaria nordinzakaria@gmail.com
// All rights reserved.

#include "Worker.h"


void Worker::copy(const Worker & orig)
 {
  if(&orig == this) return;

  prestepM = orig.prestepM;
  stepM = orig.stepM;
  poststepM = orig.poststepM;
  initM = orig.initM;
  id = orig.id;
  orig.copyJobsInto(*this);

  unsigned int sz = orig.properties_size();
  properties_size(sz); 
  for (unsigned int i=0; i<sz; i++) {
	set(orig.get(i), i);	
	}
 }

void Worker::read(FILE *infile)
	{
          unsigned int id;
	  fread(&id,sizeof(unsigned int),1,infile);
	  setID(id);

          unsigned int numprop;
	  fread(&numprop,sizeof(unsigned int),1,infile);
	  properties_size(numprop);
	  for (unsigned int i=0; i<numprop; i++)
		{
		unsigned int prop;
	  	fread(&prop,sizeof(unsigned int),1,infile);
		set(prop, i);
		}		  

          unsigned int numwjob = 0;
	  fread(&numwjob,sizeof(unsigned int),1,infile);
          for (unsigned int j=0; j<numwjob; j++) {
                JobAssigned job;
                job.read(infile);
                addJob(job);
                }
	}

void Worker::write(FILE *outfile)
    {
     fwrite(&id, sizeof(unsigned int), 1, outfile);

     unsigned int numprop = properties.size();
     fwrite(&numprop, sizeof(unsigned int), 1, outfile);
     for (unsigned int i=0; i<numprop; i++) {
     	fwrite(&(properties[i]), sizeof(unsigned int), 1, outfile);
	}

     unsigned int jsize = jobList.size();
     fwrite(&jsize, sizeof(unsigned int), 1, outfile);
     for (unsigned int i=0; i<jobList.size(); i++) {
         jobList[i].write(outfile);
        }
    }

ostream& operator<<(ostream& os, const Worker& obj)
        {
	if (os == cout)
		{
          	os << "Id= " << obj.id << ", Numprop= ";
     		unsigned int numprop = obj.properties.size();
		os << numprop << endl;
		for (unsigned int i=0; i<numprop; i++) {
			os << obj.get(i) << " ";
			}

          	os << ", Num jobs = " << obj.jobList.size() << endl;
          	for (unsigned int i=0; i<obj.jobList.size(); i++) {
                	os << "\t Job " << i << ": " << obj.jobList[i] << endl;
               		}
		}	
	else
	{
    	  os.write ((char*)&obj.id, sizeof (unsigned int));

     	  unsigned int numprop = obj.properties_size();
    	  os.write ((char*)&numprop, sizeof (unsigned int));
	  for (unsigned int i=0; i<numprop; i++) {
		unsigned int prop = obj.get(i);
    	  	os.write ((char*)&prop, sizeof (unsigned int));
		}

	  int jsize = obj.jobList.size();
    	  os.write ((char*)&jsize, sizeof (unsigned int));
          for (unsigned int i=0; i<obj.jobList.size(); i++)
               {
                os << obj.jobList[i];
               }
	}

        return os;
        }

istream& operator>>(istream& is, Worker& obj)
        {
           // read obj from stream
    	  char buffer[128];
          unsigned int id;
    	  is.read(buffer, sizeof(unsigned int));
	  id = atoi(buffer);
	  obj.setID(id);

    	  is.read(buffer, sizeof(unsigned int));
	  unsigned int numprops = atoi(buffer);
	  obj.properties_size(numprops);
	  for (unsigned int i=0; i<numprops; i++) {
    	  	is.read(buffer, sizeof(unsigned int));
		obj.set(atoi(buffer), i);
		}

          unsigned int numwjob = 0;
    	  is.read(buffer, sizeof(unsigned int));
          numwjob = atoi(buffer);
          for (unsigned int j=0; j<numwjob; j++) {
                JobAssigned job;
                is >> job;
                obj.addJob(job);
                }

          return is;
        }



